package com.youku.plugins.shareV4.view
{
   import flash.display.MovieClip;
   import com.youku.interfaces.IPlayerProxy;
   import com.youku.plugins.shareV4.view.components.SharePanel;
   import com.youku.PlayerConfig;
   import com.youku.events.PlayerControlEvent;
   import com.youku.events.ConductorEvent;
   import com.youku.plugins.shareV4.view.events.SharePanelEvent;
   import com.youku.data.PlayerConstant;
   import com.youku.plugins.shareV4.ShareConstant;
   import com.youku.events.PluginEvent;
   
   public class ShareMediator extends Object
   {
      
      public function ShareMediator(param1:MovieClip, param2:IPlayerProxy) {
         super();
         this._mainMc = param1;
         this._playerProxy = param2;
      }
      
      private var _mainMc:MovieClip;
      
      private var _playerProxy:IPlayerProxy;
      
      private var _width:Number = 0;
      
      private var _height:Number = 0;
      
      private var _barH:Number;
      
      private var _panel:SharePanel;
      
      private var _from:String;
      
      private var _isShowing:Boolean;
      
      public function init() : void {
         this._isShowing = false;
         this._from = "";
         this._width = PlayerConfig.normalWidth;
         this._height = PlayerConfig.normalHeight;
         this._panel = new SharePanel();
         this._panel.init(this._playerProxy);
         this.addEvents();
      }
      
      private function addEvents() : void {
         this._playerProxy.addEventListener(PlayerControlEvent.PLAYER_SIZE_CHANGED,this.onPlayerControlEvent);
         this._playerProxy.addEventListener(PlayerControlEvent.FULLSCREEN_CHANGED,this.onPlayerControlEvent);
         this._playerProxy.addEventListener(PlayerControlEvent.COMMON_RESET,this.onPlayerControlEvent);
         this._playerProxy.addEventListener(PlayerControlEvent.SHOW_SHARE,this.onPlayerControlEvent);
         this._playerProxy.addEventListener(PlayerControlEvent.HIDE_SHARE,this.onPlayerControlEvent);
         this._playerProxy.addEventListener(PlayerControlEvent.SHOW_END_CARD,this.onPlayerControlEvent);
         this._playerProxy.addEventListener(PlayerControlEvent.BEFORE_SHOW_PANELS,this.onPlayerControlEvent);
         this._playerProxy.addEventListener(ConductorEvent.METADATA_UPDATED,this.onMetadataUpdated);
         this._panel.addEventListener(SharePanelEvent.CLOSE,this.onPanelClose);
      }
      
      private function onPlayerControlEvent(param1:PlayerControlEvent) : void {
         var _loc2_:* = false;
         var _loc3_:* = NaN;
         var _loc4_:* = NaN;
         var _loc5_:* = NaN;
         var _loc6_:String = null;
         switch(param1.type)
         {
            case PlayerControlEvent.PLAYER_SIZE_CHANGED:
            case PlayerControlEvent.FULLSCREEN_CHANGED:
               _loc2_ = param1.data.fullscreen;
               _loc3_ = param1.data.width;
               _loc4_ = param1.data.height;
               _loc5_ = param1.data.cheight;
               this._width = _loc3_;
               this._height = _loc2_?_loc4_:_loc4_ - _loc5_;
               this.updateSize();
               break;
            case PlayerControlEvent.COMMON_RESET:
               this.hide();
               break;
            case PlayerControlEvent.SHOW_SHARE:
               if((this._isShowing) && this._from == "share" && this._from == param1.data.from)
               {
                  this.hide();
               }
               else
               {
                  this.show(param1.data.from,param1.data.time);
               }
               break;
            case PlayerControlEvent.SHOW_END_CARD:
               this.hide();
               break;
            case PlayerControlEvent.HIDE_SHARE:
               if(this._from == param1.data.from)
               {
                  this.hide();
               }
               break;
            case PlayerControlEvent.BEFORE_SHOW_PANELS:
               _loc6_ = param1.data.panel;
               if(_loc6_ != PlayerConstant.PANEL_SHARE)
               {
                  this.hide();
               }
               break;
         }
      }
      
      private function onPanelClose(param1:SharePanelEvent) : void {
         this.hide();
      }
      
      private function updateSize() : void {
         this._panel.setSize(ShareConstant.SHARE_WIDTH,ShareConstant.SHARE_HEIGHT);
         var _loc1_:Number = 1;
         var _loc2_:Number = 1;
         if(this._width < ShareConstant.SHARE_WIDTH || this._height < ShareConstant.SHARE_HEIGHT)
         {
            if(this._width / this._height < ShareConstant.SHARE_WIDTH / ShareConstant.SHARE_HEIGHT)
            {
               _loc1_ = _loc2_ = this._width / ShareConstant.SHARE_HEIGHT;
            }
            else
            {
               _loc1_ = _loc2_ = this._height / ShareConstant.SHARE_HEIGHT;
            }
         }
         if(this._width > ShareConstant.MAX_PANEL_WIDTH || this._height > ShareConstant.MAX_PANEL_HEIGHT)
         {
            if(this._width / this._height < ShareConstant.MAX_PANEL_WIDTH / ShareConstant.MAX_PANEL_HEIGHT)
            {
               _loc1_ = _loc2_ = this._height / ShareConstant.MAX_PANEL_HEIGHT;
            }
            else
            {
               _loc1_ = _loc2_ = this._width / ShareConstant.MAX_PANEL_WIDTH;
            }
         }
         this._panel.scaleX = _loc1_;
         this._panel.scaleY = _loc2_;
         this.updateLayout();
      }
      
      private function updateLayout() : void {
         this._panel.x = Math.floor((this._width - this._panel.width * this._panel.scaleX) / 2);
         this._panel.y = Math.floor((this._height - this._panel.height * this._panel.scaleY) / 2);
      }
      
      private function hide() : void {
         if(this._isShowing)
         {
            if(this._mainMc.contains(this._panel))
            {
               this._mainMc.removeChild(this._panel);
            }
            this._panel.visible = false;
            this._isShowing = false;
            this._playerProxy.dispatchEvent(new PluginEvent(PluginEvent.SHARE_CLOSE));
            this._playerProxy.dispatchEvent(new PluginEvent(PluginEvent.PANEL_STATE_CHANGE,
               {
                  "type":"share",
                  "state":false,
                  "from":this._from
               }));
         }
      }
      
      private function show(param1:String, param2:Number) : void {
         this._from = param1;
         if(!this._mainMc.contains(this._panel))
         {
            this._mainMc.addChild(this._panel);
         }
         this._panel.show(param1,param2);
         this._panel.visible = true;
         this._isShowing = true;
         this._playerProxy.dispatchEvent(new PluginEvent(PluginEvent.PANEL_STATE_CHANGE,
            {
               "type":"share",
               "state":true,
               "from":param1
            }));
      }
      
      private function onMetadataUpdated(param1:ConductorEvent) : void {
      }
   }
}
